package com.sajus.web.billing.admin.action;

import java.util.Date;
import java.util.List;

import javax.naming.NamingException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sajus.billing.client.delegate.BusinessDelegateFactory;
import com.sajus.billing.client.dto.OfferingDTO;
import com.sajus.billing.client.dto.PromoOfferingDTO;
import com.sajus.billing.client.dto.PromotionDTO;
import com.sajus.billing.client.dto.ServiceTypeDTO;

/**
 * Acts as a Struts 2 controller that responds
 * to a user action by setting the value
 * of the Message model class, and returns a String 
 * result.
 * @author Bruce Phillips
 *
 */
public class PromoOfferingAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private final String PROMOTION_ID = "promotionId";
	/**
	 * The model class that stores the message
	 * to display in the view.
	 */
	private List<PromoOfferingDTO> promoOfferings;

	private PromoOfferingDTO offering;

	private PromotionDTO promotion;
	

	/*
	 * Creates the MessageStore model object and 
	 * returns success.  The MessageStore model
	 * object will be available to the view.
	 * (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception {
		
		if ( this.offering != null ) {
			
			System.out.println("Offering id " + this.offering.getId() );
			System.out.println("Offering description " + this.offering.getDescription() );

			if ( this.offering.getId() == null ){
				if ( this.offering.getDescription() != null ){
					System.out.println("Offering is being saved" );
					this.offering.setPromotion( promotion );
					BusinessDelegateFactory.getInstance().getServiceTypeManager().savePromoOffering( this.offering );
					return SUCCESS;
				}
			} else {
				this.offering.setPromotion( promotion );
				BusinessDelegateFactory.getInstance().getServiceTypeManager().savePromoOffering( this.offering );
				System.out.println("Persist object Success reset value" );
				return SUCCESS;
			}
		}
		
		if ( this.offering == null ){
			System.out.println("Initializing Promo Offering");
			offering = new PromoOfferingDTO();
			
			String offeringParam = ServletActionContext.getRequest().getParameter("offeringId");
			System.out.println( "Offering " + offeringParam );
			
			if ( offeringParam != null ){
				this.offering = BusinessDelegateFactory.getInstance()
						.getServiceTypeManager().getPromotionOffering( Integer.parseInt( offeringParam ) );
			}
			
		} else {
			System.out.println("Promo Offering is not null ");
		}
		
		if ( this.promotion == null ){
			
			String paramValue = ServletActionContext.getRequest().getParameter("promotionId");
			System.out.println(" ParamValue = " + paramValue );
			
			if ( paramValue != null ){
				ServletActionContext.getRequest().setAttribute(PROMOTION_ID, paramValue );
			} else {
				paramValue = (String) ServletActionContext.getRequest().getAttribute( PROMOTION_ID );
			}
			
			if ( paramValue == null )
				return SUCCESS;
			
			this.promotion = BusinessDelegateFactory.getInstance()
					.getServiceTypeManager().getPromotion( Integer.parseInt( paramValue ) );

		} else {
			System.out.println("promotion is not null ");
		}
		
		
		System.out.println(" Promotion " + promotion.getDescription() );
		this.offering.setPromotion( this.promotion );
		
		this.promoOfferings = BusinessDelegateFactory.getInstance()
				.getServiceTypeManager().getPromoOfferings( promotion );
		
		if ( this.promoOfferings != null ){
			for( PromoOfferingDTO tmpPro : this.promoOfferings ){
				tmpPro.setPromotion( this.promotion );
			}
		}

		if ( this.promoOfferings != null)
			System.out.println(" size of the promo offering " + this.promoOfferings.size() );

		return INPUT;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public List<PromoOfferingDTO> getPromoOfferings() {
		return promoOfferings;
	}


	public void setPromoOfferings(List<PromoOfferingDTO> promoOfferings) {
		this.promoOfferings = promoOfferings;
	}

	public List<OfferingDTO> getOfferings(){
		try {
			return BusinessDelegateFactory.getInstance().getServiceTypeManager().getOfferings();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public List<OfferingDTO> getOfferingsTwo(){
		try {
			return BusinessDelegateFactory.getInstance().getServiceTypeManager().getOfferings();
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return null;
	}
	

	public PromotionDTO getPromotion() {
		return promotion;
	}


	public void setPromotion(PromotionDTO promotion) {
		this.promotion = promotion;
	}


	public OfferingDTO getInitialOffering(){
		if ( this.offering != null )
			return this.offering.getInitialOffering();
		
		return null;
	}
	
	public OfferingDTO getFinalOffering(){
		if ( this.offering != null )
			return this.offering.getFinalOffering();
		
		return null;
	}
	
	public void setInitialOffering( Integer id ){
		if ( this.offering == null ){
			this.offering = new PromoOfferingDTO();
		}
		try {
			System.out.println("Setting InitialOffering " + id );
			this.offering.setInitialOffering( BusinessDelegateFactory.getInstance()
					.getServiceTypeManager().getOffering( id ) );
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}
	
	public void setFinalOffering( Integer id ){
		if ( this.offering == null ){
			this.offering = new PromoOfferingDTO();
		}
		try {
			System.out.println("Setting FinalOffering " + id );
			this.offering.setFinalOffering( BusinessDelegateFactory.getInstance()
					.getServiceTypeManager().getOffering( id ) );
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}


	public PromoOfferingDTO getOffering() {
		return offering;
	}


	public void setOffering(PromoOfferingDTO offering) {
		this.offering = offering;
	}
	
	public Integer getOfferingPromotion(){
		if ( this.promotion != null ){
			return this.promotion.getId();
		}
		
		return null;
	}
	
	public void setOfferingPromotion( String ids ){
		Integer id = new Integer( Integer.parseInt(ids) );
		
		if ( this.promotion == null ){
			try {
				this.promotion = BusinessDelegateFactory.getInstance()
						.getServiceTypeManager().getPromotion( id );
			} catch (NamingException e) {
				e.printStackTrace();
			}
		}
	}

}
